home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / ICProgKit1.0 / Examples / IC2Resource / IC2Resource.c < prev    next >
C/C++ Source or Header  |  1994-11-29  |  3KB  |  116 lines

  1. #include "ICAPI.h"
  2. #include "ICKeys.h"
  3. #include "ICMappings.h"
  4. #include "ICSubs.h"
  5.  
  6. typedef struct {
  7.     unsigned char    extension[6];
  8.     OSType    fType;
  9.     OSType    fCreator;
  10.     short    reserved;
  11. }typeRec;
  12.  
  13. typedef struct {
  14.     short    numTypes;
  15.     typeRec    theTypes[1];
  16. }typeListRec, **typeListRecH;
  17.  
  18. void InitToolbox (void) 
  19. {
  20.     InitGraf ( &qd.thePort );
  21.     InitFonts ();
  22.     InitWindows ();
  23.     InitMenus ();
  24.     TEInit ();
  25.     InitDialogs (NULL);        // use of ResumeProcs no longer approved by Apple
  26.     InitCursor ();
  27.     FlushEvents ( everyEvent, 0 );
  28.     
  29.     // how about some memory fun! Two should be enough!
  30.     MoreMasters ();
  31.     MoreMasters ();
  32. }
  33.  
  34.  
  35. void main(void)
  36. {
  37.     ICInstance        inst;
  38.     ICAttr            attr = 0;
  39.     ICError         err;
  40.     ICMapEntry        entry;
  41.     Handle            mappings = nil;
  42.     long            prefsize = 0;
  43.     typeListRecH    tlrH = nil;
  44.     SFTypeList         typeList;
  45.     StandardFileReply    reply;
  46.     
  47.     InitToolbox();
  48.     
  49.     StandardGetFile(nil, -1, typeList, &reply);    // standardfile.h
  50.     if (!reply.sfGood)    return;
  51.     
  52.     if (!ICStart(&inst, 'ICMa')) {    // init the sucker
  53.         if (!ICFindConfigFile(inst, 0, nil)) {    // locate the config file
  54.             if (!ICBegin(inst, icReadOnlyPerm)) {    // open up the file for reading
  55.                 if (!ICGetPrefHandle(inst, (ConstStr255Param)kICMapping, &attr, &mappings)) {    // get the size
  56.                     long         pos = 0, count, i = 0;
  57.  
  58.                     // first things first - found out how many to preflight memory allocation                    
  59.                     err = ICMCountEntries(mappings, &count);
  60.                     if (!err && count) {
  61.                         tlrH = (typeListRecH)NewHandle(count * sizeof(typeRec) + sizeof(short));
  62.                         if (tlrH) {
  63.                             HLock((Handle)tlrH);
  64.                             (*tlrH)->numTypes = count-1;    // -1 for 0 based count
  65.                             
  66.                             err = ICMGetEntry(mappings, pos, &entry);
  67.                             do {
  68.                                 // copy the info out to the typeListH
  69.                                 BlockMoveData(entry.extension, (*tlrH)->theTypes[i].extension, 6);
  70.                                 (*tlrH)->theTypes[i].fType = entry.file_type;
  71.                                 (*tlrH)->theTypes[i].fCreator = entry.file_creator;
  72.                                 (*tlrH)->theTypes[i].reserved = 1;
  73.                                 
  74.                                 // and get the next one!
  75.                                 i++;
  76.                                 pos += entry.total_length;
  77.                                 err = ICMGetEntry(mappings, pos, &entry);
  78.                             } while (err == noErr);
  79.                             
  80.                             // open up the file and add the resource
  81.                             {
  82.                                 short refNum = FSpOpenResFile(&reply.sfFile, fsRdWrPerm);    // resources.h
  83.                                 Handle    tmpH = Get1Resource('E2Ty', 0);
  84.                                 
  85.                                 if (tmpH) {
  86.                                     SetHandleSize(tmpH, GetHandleSize((Handle)tlrH));
  87.                                     BlockMoveData((Ptr)*tlrH, *tmpH, GetHandleSize((Handle)tlrH));
  88.                                     ChangedResource(tmpH);
  89.                                     WriteResource(tmpH);
  90.                                     DisposHandle((Handle)tlrH);
  91.                                 } else {
  92.                                     AddResource((Handle)tlrH, 'E2Ty', 0, "\p");
  93.                                     WriteResource((Handle)tlrH);
  94.                                     ReleaseResource((Handle)tlrH);
  95.                                 }
  96.                                 CloseResFile(refNum);
  97.                             }
  98.                         } else
  99.                             err = MemError();
  100.                     }
  101.                     
  102.                     DisposHandle(mappings);
  103.                 } else
  104.                     StopAlert(129, 0L);
  105.                 
  106.                 ICEnd(inst);    // all done
  107.             } else
  108.                 StopAlert(129, 0L);
  109.         } else
  110.             StopAlert(129, 0L);
  111.         
  112.         ICStop(inst);            // all done
  113.     } else
  114.         StopAlert(128, 0L);
  115. }
  116.